<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="4" :md="12">
            <el-form-item label="资源位名称:" label-width="120px">
              <el-input
                clearable
                v-model="where.title"
                placeholder="请输入资源位名称"/>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="开启状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="全部"
                class="ele-fluid">
                <el-option :label="'启用'" :value="1" :key="1"/>
                <el-option :label="'禁用'" :value="0" :key="0"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="投放设备:">
              <el-select clearable v-model="where.device" placeholder="全部" class="ele-fluid">
                <el-option :label="'全投放'" :value="1" :key="1"/>
                <el-option :label="'IOS'" :value="2" :key="2"/>
                <el-option :label="'安卓'" :value="3" :key="3"/>
                <el-option :label="'PAD'" :value="4" :key="4"/>
                <el-option :label="'IPAD'" :value="5" :key="5"/>
                <el-option :label="'PC'" :value="6" :key="6"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="日期:">
              <el-date-picker
                style="width: 100%"
                v-model="where.create_time"
                type="datetimerange"
                value-format="yyyy-MM-dd HH-mm-ss"
                label-format="yyyy-MM-dd HH-mm-ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)">添加
          </el-button>
        </template>

        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)">修改
          </el-link>
        </template>

        <template slot="icon" slot-scope="{row}">
          <el-image v-if="row.icon == ''" style="width: 30px; height: 30px">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <el-image v-else style="width: 30px; height: 30px" :src="row.icon" :preview-src-list="[row.icon]"
                    fit="fit"></el-image>
        </template>
<!--        <template slot="device" slot-scope="{ row }">-->
<!--          <span v-for="(value, index) in row.device.split(',')" :key="index" style="padding-left: 8px;">-->
<!--            <el-tag size="mini" v-if="value == ''">暂无</el-tag>-->
<!--            <el-tag size="mini" v-else :type="['success', 'primary', 'warning', 'info'][index - 1]">{{-->
<!--                ['全部', 'IOS', '安卓', 'PAD', 'IPAD', 'PC'][value - 1]-->
<!--              }}</el-tag>-->
<!--          </span>-->
<!--        </template>-->

        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="status(row)"
            :active-value="1"
            :inactive-value="0"/>
        </template>
        <template slot="select_icon" slot-scope="{ row }">
          <el-image v-if="row.select_icon == ''" style="width: 30px; height: 30px">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <el-image v-else style="width: 30px; height: 30px" :src="row.select_icon"
                    :preview-src-list="[row.select_icon]" fit="fit"></el-image>
        </template>

      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import Edit from './edit.vue';

export default {
  name: 'MyServiceIndex',
  components: {Edit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/boxResourceMyservice/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'title',
          label: '资源位名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'icon',
          label: '缩略图',
          showOverflowTooltip: false,
          minWidth: 200,
          maxHeight: 10,
          align: 'center',
          slot: 'icon',
        },
        {
          prop: 'select_icon',
          label: '选中图',
          showOverflowTooltip: false,
          minWidth: 200,
          maxHeight: 10,
          align: 'center',
          slot: 'select_icon',
        },
        // {
        //   prop: 'device',
        //   label: '投放设备',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center',
        //   slot: 'device',
        // },
        {
          prop: 'version',
          label: '版本号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$convertNumberToVersion(cellValue);
          },
        },
        {
          prop: 'link',
          label: '链接(URL)',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'sort',
          label: '排序（权重）',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'status',
          label: '资源位状态',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot: 'status',
        },
        {
          prop: 'notes',
          label: '备注',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },

  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('boxResourceMyservice/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 0;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
